package it.softphone.cmc.server.entity;

import it.softphone.cmc.shared.entity.Attributes;
import it.softphone.commons.json.server.JSONException;
import it.softphone.commons.service.LogAware;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Embeddable;



@SuppressWarnings("serial")
@Embeddable
public class AttributesEntity implements Attributes, Serializable, LogAware {
	
	private Date updated;
	
	private com.google.appengine.api.datastore.Text jsonPayload = new com.google.appengine.api.datastore.Text("{}");
	
	public AttributesEntity() {
		updated = new Date();
	}

	public Date getUpdated() {
		return updated;
	}

	public void setUpdated(Date updated) {
		this.updated = updated;
	}


	@Override
	public String getJSonPayload() {
		return jsonPayload.getValue();
	}
	
	@Override
	public void setJSonPayload( String payload ) {
		jsonPayload = new com.google.appengine.api.datastore.Text(payload); 
	}
	
	protected final it.softphone.commons.json.server.JSONObject encodeAttributes()  {
	
		it.softphone.commons.json.server.JSONObject result = null;
		
		try {
			result = new it.softphone.commons.json.server.JSONObject( getJSonPayload() );
		} catch (JSONException e) {
			// TODO log exception
		}
		
		return result;
	}
	
	protected final void decodeAttributes( Object object ) {
		
		setJSonPayload( String.valueOf(object));
	}

}
